package com.situ.day250121;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionDemo {
    @Test
    public void test() {
        //以下是运行时异常
        String str= "qwe";
        System.out.println(str.charAt(1));
        //System.out.println(str.charAt(3));
        // --StringIndexOutofBoundsException 字符串下标越界异常
        //Exception exception = new ArithmeticException();
        //System.out.println(3 / 0);  //ArithmeticException 算数异常
        /*try {ctrl+alt+t
            System.out.println(3 / 0);//但这是写的代码的错误，不要try了
        } catch (Exception e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }*/
        //以下是编译时异常，未雨绸缪
        try {//里面放的是需要检查的代码，可能会抛出异常，也可能不抛出
            FileInputStream fileInputStream = new FileInputStream("a1.txt");//在demo01下建a.txt才不抛出异常
        } catch (FileNotFoundException e) {
            //throw new RuntimeException(e);捕获后抛出异常，抛出的话不打印
            e.printStackTrace();
        }finally {
            //一定会执行的代码，不管抛不抛出异常
            System.out.println("finally");
        }

        System.out.println("ExceptionDemo.test1");
    }
    @Test
    public void test1() {
        //加载JDBC驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }//try catch后不抛出后面还能打印
        System.out.println("ExceptionDemo.test2");
    }

    @Test
    public void test2() {
        try {
            show();
        } catch (ClassNotFoundException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }
    }

    public void show() throws ClassNotFoundException {//此处抛出谁调用谁处理,上面还得跑
        Class.forName("com.mysql.jdbc.Driver");
    }

    @Test
    public void test3() throws IOException {//此处的IO抛的是read的（68）行
        try {//文件输入流
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            fileInputStream.read();//如果不抛继续catch就可以有多个catch,如下
        } catch (FileNotFoundException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }
    }

    @Test//多重捕获块
    public void test4() {//此处的IO抛的是read的（68）行
        try {//文件输入流
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            fileInputStream.read();//如果不抛继续catch就可以有多个catch,如下
        } catch (FileNotFoundException e) {//两个catch不能颠倒顺序，前面的是子类
            //throw new RuntimeException(e);
            e.printStackTrace();
        } catch (IOException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        }finally {
            System.out.println("ExceptionDemo.test3");
        }
    }

    @Test
    public void test5() {
        try {
            quQian(2000);
        } catch (ZhuanZhangException e) {//ctrl
            //throw new RuntimeException(e);抛出的话后面不再执行
            e.printStackTrace();
        }

        System.out.println("ExceptionDemo.test55");
    }

    //withdraw
    public void quQian(double money) throws ZhuanZhangException {//方法的签名上抛出异常，里面就调用了throw new异常
        if (money >= 1000) {
            throw new ZhuanZhangException("钱不够了");
        }

        System.out.println("钱够了");
    }
}
